package de.karlNet.sambamanager.ldapModel;

import java.util.ArrayList;
import java.util.List;

public class LdapObject implements ITypeChecker {
	private String cn;
	private String dn;
	private String originalDN;
	private String description;
	private List<String> members = new ArrayList<String>();
	private List<String> memberOf = new ArrayList<String>();

	public LdapObject() {

	}

	public LdapObject(String cn, String dn) {
		this.cn = cn;
		this.dn = (dn==null?null:dn.toLowerCase());
	}

	@Override
	public boolean equals(Object obj) {
		
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		LdapObject other = (LdapObject) obj;
		if(other.dn.equals(this.dn)) {
			return true;
		}
		if (cn == null) {
			if (other.cn != null)
				return false;
		} else if (!cn.equals(other.cn))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (dn == null) {
			if (other.dn != null)
				return false;
		} else if (!dn.equals(other.dn))
			return false;
		if (memberOf == null) {
			if (other.memberOf != null)
				return false;
		} else if (!memberOf.equals(other.memberOf))
			return false;
		if (members == null) {
			if (other.members != null)
				return false;
		} else if (!members.equals(other.members))
			return false;
		if (originalDN == null) {
			if (other.originalDN != null)
				return false;
		} else if (!originalDN.equals(other.originalDN))
			return false;
		return true;
	}

	public String getCn() {
		return cn;
	}

	public String getDescription() {
		return description;
	}

	public String getDn() {
		return dn.toLowerCase();
	}

	public List<String> getMemberOf() {
		return memberOf;
	}

	public List<String> getMembers() {
		return members;
	}

	public String getOriginalDN() {
		return originalDN;
	}

	public String getParentDn() {
		String parentPath = "";
		String[] components = this.getDn().split(",");

		String concatElement = "";
		boolean skipFirst = true;
		for (String component : components) {
			if (skipFirst) {
				skipFirst = false;
				continue;
			}
			String[] subComponents = component.split("=");
				parentPath = parentPath + concatElement + subComponents[0]
						+ "=" + subComponents[1];
			concatElement = ",";
		}
		return parentPath;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((cn == null) ? 0 : cn.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((dn == null) ? 0 : dn.hashCode());
		result = prime * result
				+ ((memberOf == null) ? 0 : memberOf.hashCode());
		result = prime * result + ((members == null) ? 0 : members.hashCode());
		result = prime * result
				+ ((originalDN == null) ? 0 : originalDN.hashCode());
		return result;
	}

	@Override
	public void isType(ITypeCheckerCallback typeCheckerCallback) {
		typeCheckerCallback.isLdapObject();
		typeCheckerCallback.isLdapObject(this);
	}

	public void setCn(String cn) {
		this.cn = cn;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setDn(String dn) {
		this.dn = dn.toLowerCase();
	}

	public void setOriginalDN(String originalDN) {
		this.originalDN = originalDN;
	}

	@Override
	public String toString() {
		return "LdapObject [cn=" + cn + ", dn=" + dn + ", originalDN="
				+ originalDN + "]";
	}
}
